/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package BeanView;

import HibernateUtil.HibernateUtil;
import Pojo.Addrobj;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.faces.event.AjaxBehaviorEvent;
import javax.faces.event.ValueChangeEvent;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

/**
 *
 * @author olegk
 */
@ManagedBean
@ViewScoped
//@RequestScoped
public class MbVAddress implements java.io.Serializable{
    private Session session;
    private Transaction transaction;
    
    private List<Addrobj> regions;
    private List<Addrobj> countys;
    private List<Addrobj> districts;
    private List<Addrobj> citys;
    private List<Addrobj> innerCitys;
    private List<Addrobj> localitys;
    private List<Addrobj> streets;
    private List<Addrobj> addGrounds;
    private List<Addrobj> streetAddGrounds;
    private String regionLevel;
    private String countyLevel;
    private String districtLevel;
    private String cityLevel;
    private String innerCityLevel;
    private String localityLevel;
    private String streetLevel;
    private String addGroundLevel;
    private String streetAddGroundLevel;
     
    @PostConstruct
    public void init() {
        regions  = ListAOLevel("select * from CONTRACT.ADDROBJ a where a.aolevel=1");
        regionLevel = FindDefaultRegionCode("23", regions);
        System.out.println(regionLevel);
        onRegionChange();
    }
        
    public void onRegionChange() {
        if(regionLevel !=null && !regionLevel.equals("")){            
            countys = ListAOLevel("select * from CONTRACT.ADDROBJ a where a.ParentGUID = '"+regionLevel+"' and a.aolevel=2");
            districts = ListAOLevel("select * from CONTRACT.ADDROBJ a where a.ParentGUID = '"+regionLevel+"' and a.aolevel=3");
            citys = ListAOLevel("select * from CONTRACT.ADDROBJ a where a.ParentGUID = '"+regionLevel+"' and a.aolevel=4");
        }else{
            countys = new ArrayList<Addrobj>();
            districts = new ArrayList<Addrobj>();
            citys = new ArrayList<Addrobj>();
        }    
    }
    
    public void onDistrictChange() {
        if(districtLevel !=null && !districtLevel.equals("")){            
            citys = ListAOLevel("select * from CONTRACT.ADDROBJ a where a.ParentGUID = '"+districtLevel+"' and a.aolevel=4");
            innerCitys = ListAOLevel("select * from CONTRACT.ADDROBJ a where a.ParentGUID = '"+districtLevel+"' and a.aolevel=5");
            localitys = ListAOLevel("select * from CONTRACT.ADDROBJ a where a.ParentGUID = '"+districtLevel+"' and a.aolevel=6");
        }else{
            citys = new ArrayList<Addrobj>();
            innerCitys = new ArrayList<Addrobj>();
            localitys = new ArrayList<Addrobj>();
            onRegionChange();
        }    
    }
    
    public void onCityChange() {
        if(cityLevel !=null && !cityLevel.equals("")){            
            innerCitys = ListAOLevel("select * from CONTRACT.ADDROBJ a where a.ParentGUID = '"+cityLevel+"' and a.aolevel=5");
            localitys = ListAOLevel("select * from CONTRACT.ADDROBJ a where a.ParentGUID = '"+cityLevel+"' and a.aolevel=6");
            streets = ListAOLevel("select * from CONTRACT.ADDROBJ a where a.ParentGUID = '"+cityLevel+"' and a.aolevel=7");
        }else{
            innerCitys = new ArrayList<Addrobj>();
            localitys = new ArrayList<Addrobj>();
            streets = new ArrayList<Addrobj>();
            onDistrictChange();
        }    
    }
  
    public void onInnerCityChange() {
        if(innerCityLevel !=null && !innerCityLevel.equals("")){            
            streets = ListAOLevel("select * from CONTRACT.ADDROBJ a where a.ParentGUID = '"+innerCityLevel+"' and a.aolevel=7");
        }else{
            streets = new ArrayList<Addrobj>();
            onCityChange();
        }    
    }
    
    public void onLocalityChange() {
        if(localityLevel !=null && !localityLevel.equals("")){            
            streets = ListAOLevel("select * from CONTRACT.ADDROBJ a where a.ParentGUID = '"+localityLevel+"' and a.aolevel=7");
            addGrounds = ListAOLevel("select * from CONTRACT.ADDROBJ a where a.ParentGUID = '"+localityLevel+"' and a.aolevel=9");
        }else{
            streets = new ArrayList<Addrobj>();
            addGrounds = new ArrayList<Addrobj>();
            onCityChange();
            onInnerCityChange();
        }    
    }
    
    public void onAddGroundChange() {
        if(addGroundLevel !=null && !addGroundLevel.equals("")){
            streetAddGrounds = ListAOLevel("select * from CONTRACT.ADDROBJ a where a.ParentGUID = '"+addGroundLevel+"' and a.aolevel=10");
        }else{
            streetAddGrounds = new ArrayList<Addrobj>();
        }    
    }
    
    private String FindDefaultRegionCode(String numRegion, List<Addrobj> list){
        for(Addrobj i : list){
            if(i.getRegioncode().equals(numRegion)){
                return i.getAoguid();
            }
        }
        return null;
    }
 
    private List<Addrobj> ListAOLevel(String dbQuery) {
        
        this.session=null;
        this.transaction=null;        
        try
        {
            this.session=HibernateUtil.getSessionFactory().openSession();
            this.transaction=session.beginTransaction();
            Query query = session
                .createSQLQuery(dbQuery)
                .addEntity(Addrobj.class); 
            return query.list();
        }
        catch(Exception ex)
        {
            if(this.transaction!=null)
            {
                this.transaction.rollback();
            }            
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_FATAL, "Фатальная ошибка:", "Пожалуйста, обратитесь к администратору "+ex.getMessage()));
        }
        finally
        {
            if(this.session!=null)
            {
                this.session.close();
            }
        }
        return null;
    }

    public String getRegionLevel() {
        return regionLevel;
    }

    public void setRegionLevel(String regionLevel) {
        this.regionLevel = regionLevel;
    }

    public String getCountyLevel() {
        return countyLevel;
    }

    public void setCountyLevel(String countyLevel) {
        this.countyLevel = countyLevel;
    }

    public String getDistrictLevel() {
        return districtLevel;
    }

    public void setDistrictLevel(String districtLevel) {
        this.districtLevel = districtLevel;
    }

    public String getCityLevel() {
        return cityLevel;
    }

    public void setCityLevel(String cityLevel) {
        this.cityLevel = cityLevel;
    }

    public String getInnerCityLevel() {
        return innerCityLevel;
    }

    public void setInnerCityLevel(String innerCityLevel) {
        this.innerCityLevel = innerCityLevel;
    }

    public String getLocalityLevel() {
        return localityLevel;
    }

    public void setLocalityLevel(String localityLevel) {
        this.localityLevel = localityLevel;
    }

    public String getStreetLevel() {
        return streetLevel;
    }

    public void setStreetLevel(String streetLevel) {
        this.streetLevel = streetLevel;
    }

    public String getAddGroundLevel() {
        return addGroundLevel;
    }

    public void setAddGroundLevel(String addGroundLevel) {
        this.addGroundLevel = addGroundLevel;
    }

    public String getStreetAddGroundLevel() {
        return streetAddGroundLevel;
    }

    public void setStreetAddGroundLevel(String streetAddGroundLevel) {
        this.streetAddGroundLevel = streetAddGroundLevel;
    }

    public List<Addrobj> getRegions() {
        return regions;
    }

    public List<Addrobj> getCountys() {
        return countys;
    }

    public List<Addrobj> getDistricts() {
        return districts;
    }

    public List<Addrobj> getCitys() {
        return citys;
    }

    public List<Addrobj> getInnerCitys() {
        return innerCitys;
    }

    public List<Addrobj> getLocalitys() {
        return localitys;
    }

    public List<Addrobj> getStreets() {
        return streets;
    }

    public List<Addrobj> getAddGrounds() {
        return addGrounds;
    }

    public List<Addrobj> getStreetAddGrounds() {
        return streetAddGrounds;
    }
    
    
    

    
    
    
    
    
    
    
    
    
    
    
    
}
